home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995…tember: Reference Library / Dev.CD Sep 95 RL / Dev.CD Sep 95 RL.toast / mac / Technical Documentation / develop / develop Issue 23 code / Internet Config / IC 1.1 / ICProgKit1.1 / Source / Internet Config Source / ICRSubs.p < prev   
Encoding:
Text File  |  1995-04-23  |  8.6 KB  |  317 lines  |  [TEXT/PJMM]

  1. unit ICRSubs;
  2.  
  3. interface
  4.  
  5.     uses
  6. {$ifc undefined THINK_Pascal}
  7.         Types, Files, 
  8. {$endc}
  9.         ICTypes;
  10.  
  11.     function EditPreferences (key: Str255; prefsfile: FSSpec): ICError;
  12.  
  13.     function FindScheme (urlh: Handle; var scheme: Str255): ICError;
  14.     function LaunchURL (helper: OSType; urlh: Handle): ICError;
  15.  
  16. implementation
  17.  
  18.     uses
  19. {$ifc undefined THINK_Pascal}
  20.         GestaltEqu, Errors, ToolUtils, 
  21. {$endc}
  22.         Processes, AppleEvents;
  23.  
  24.     function HaveProcessManager: ICError;
  25.         var
  26.             gv: longint;
  27.     begin
  28.         if (Gestalt(gestaltOSAttr, gv) = noErr) & (BTST(gv, gestaltLaunchControl)) then begin
  29.             HaveProcessManager := noErr;
  30.         end
  31.         else begin
  32.             HaveProcessManager := unimpErr;
  33.         end; (* if *)
  34.     end; (* HaveProcessManager *)
  35.  
  36.     function FindProcess (creator, typ: OSType; var process: ProcessSerialNumber; var fs: FSSpec): boolean;
  37.         var
  38.             info: ProcessInfoRec;
  39.             oe: OSErr;
  40.             gv: longInt;
  41.     begin
  42.         FindProcess := false;
  43.         if HaveProcessManager = noErr then begin
  44.             process.highLongOfPSN := 0;
  45.             process.lowLongOfPSN := kNoProcess;
  46.             info.processInfoLength := sizeof(ProcessInfoRec);
  47.             info.processName := nil;
  48.             info.processAppSpec := @fs;
  49.             while GetNextProcess(process) = noErr do begin
  50.                 if (GetProcessInformation(process, info) = noErr) & (info.processType = longInt(typ)) & (info.processSignature = creator) then begin
  51.                     FindProcess := true;
  52.                     leave;
  53.                 end; (* if *)
  54.             end; (* while *)
  55.         end; (* if *)
  56.     end; (* FindProcess *)
  57.  
  58.     function GetVolInfo (var name: str63; var vrn: integer; index: integer): OSErr;
  59.         var
  60.             pb: paramBlockRec;
  61.             oe: OSErr;
  62.     begin
  63.         if (name <> '') & (name[length(name)] <> ':') then begin
  64.             name := concat(name, ':');
  65.         end; (* if *)
  66.         pb.ioNamePtr := @name;
  67.         pb.ioVRefNum := vrn;
  68.         pb.ioVolIndex := index;
  69.         oe := PBGetVInfo(@pb, false);
  70.         if oe = noErr then begin
  71.             vrn := pb.ioVRefNum;
  72.         end; (* if *)
  73.         GetVolInfo := oe;
  74.     end; (* GetVolInfo *)
  75.  
  76.     function ConfirmAppl (creator: OSType; var fss: FSSpec): OSErr;
  77.         var
  78.             err: OSErr;
  79.             info: FInfo;
  80.     begin
  81.         err := HGetFInfo(fss.vRefNum, fss.parID, fss.name, info);
  82.         if err = noErr then begin
  83.             if (info.fdType <> 'APPL') or (info.fdCreator <> creator) then begin
  84.                 err := afpItemNotFound;
  85.             end; (* if *)
  86.         end; (* if *)
  87.         ConfirmAppl := err;
  88.     end; (* ConfirmAppl *)
  89.  
  90.     function ScanVolume (creator: OSType; vref: integer; var fs: FSSpec): OSErr;
  91.         var
  92.             err: OSErr;
  93.             file_index: integer;
  94.             pbdt: DTPBRec;
  95.             found: boolean;
  96.     begin
  97.         fs.name := '';
  98.         pbdt.ioNamePtr := @fs.name;
  99.         pbdt.ioVRefNum := vref;
  100.         err := PBDTGetPath(@pbdt);
  101.         if err = noErr then begin
  102.             file_index := 1;
  103.             found := false;
  104.             repeat
  105.                 pbdt.ioIndex := file_index;
  106.                 pbdt.ioFileCreator := creator;
  107.                 err := PBDTGetAPPLSync(@pbdt);
  108.                 if err = noErr then begin
  109.                     fs.vRefNum := vref;
  110.                     fs.parID := pbdt.ioAPPLParID;
  111.                     (* name is already put in by GetAPPL call *)
  112.                     found := (ConfirmAppl(creator, fs) = noErr);
  113.                 end; (* if *)
  114.                 file_index := file_index + 1;
  115.             until found or (err <> noErr);
  116.         end; (* if *)
  117.         ScanVolume := err;
  118.     end; (* ScanVolume *)
  119.  
  120.     function FindApplication (creator: OSType; var fs: FSSpec): OSErr;
  121.         var
  122.             err: OSErr;
  123.             vol_index: integer;
  124.             vref: integer;
  125.             found: boolean;
  126.     begin
  127.         found := false;
  128.         vol_index := 1;
  129.         repeat
  130.             vref := 0;
  131.             err := GetVolInfo(fs.name, vref, vol_index);
  132.             if err = noErr then begin
  133.                 err := ScanVolume(creator, vref, fs);
  134.                 if err = noErr then begin
  135.                     found := true;
  136.                 end
  137.                 else begin
  138.                     err := noErr;        (* swallow error so we continue with next volume *)
  139.                 end; (* if *)
  140.             end; (* if *)
  141.             vol_index := vol_index + 1;
  142.         until found or (err <> noErr);
  143.         if not found then begin
  144.             err := afpItemNotFound;
  145.             fs.vRefNum := 0;
  146.             fs.parID := 2;
  147.             fs.name := '';
  148.         end; (* if *)
  149.         FindApplication := err;
  150.     end; (* FindApplication *)
  151.  
  152.     function PrepareToLaunch (var theEvent: AppleEvent; tofront: boolean; var launchThis: LaunchParamBlockRec): ICError;
  153.         var
  154.             launchDesc: AEDesc;
  155.     begin
  156.         PrepareToLaunch := AECoerceDesc(theEvent, typeAppParameters, launchDesc);
  157.         HLock(handle(theEvent.dataHandle));
  158.         launchThis.launchAppParameters := AppParametersPtr(launchDesc.dataHandle^);
  159.         launchThis.launchBlockID := extendedBlock;
  160.         launchThis.launchEPBLength := extendedBlockLen;
  161.         launchThis.launchFileFlags := 0;
  162.         launchThis.launchControlFlags := launchContinue + launchNoFileFlags;
  163.         if not tofront then begin
  164.             launchThis.launchControlFlags := launchThis.launchControlFlags + launchDontSwitch;
  165.         end; (* if *)
  166.     end; (* PrepareToLaunch *)
  167.  
  168.     function CreateGURLEvent (creator: OSType; urlh: Handle; var theEvent: AppleEvent): ICError;
  169.         var
  170.             targetAddress: AEDesc;
  171.             err: ICError;
  172.             junk: ICError;
  173.             err2: ICError;
  174.             s: signedByte;
  175.     begin
  176.         err := AECreateDesc(typeApplSignature, @creator, sizeof(creator), targetAddress);
  177.         err2 := AECreateAppleEvent('GURL', 'GURL', targetAddress, kAutoGenerateReturnID, kAnyTransactionID, theEvent);
  178.         if err = noErr then begin
  179.             err := err2;
  180.         end; (* if *)
  181.         s := HGetState(urlh);
  182.         HLock(urlh);
  183.         err2 := AEPutKeyPtr(theEvent, keyDirectObject, typeChar, urlh^, GetHandleSize(urlh));
  184.         HSetState(urlh, s);
  185.         if err = noErr then begin
  186.             err := err2;
  187.         end; (* if *)
  188.         if err <> noErr then begin
  189.             junk := AEDisposeDesc(theEvent);
  190.         end; (* if *)
  191.         junk := AEDisposeDesc(targetAddress);
  192.         CreateGURLEvent := err;
  193.     end; (* CreateGURLEvent *)
  194.  
  195.     function CreateEditPrefEvent (creator: OSType; key: Str255; prefsfile: FSSpec; var theEvent: AppleEvent): ICError;
  196.         var
  197.             targetAddress: AEDesc;
  198.             err: ICError;
  199.             junk: ICError;
  200.             err2: ICError;
  201.     begin
  202.         err := AECreateDesc(typeApplSignature, @creator, sizeof(creator), targetAddress);
  203.         err2 := AECreateAppleEvent('ICAp', 'ICAp', targetAddress, kAutoGenerateReturnID, kAnyTransactionID, theEvent);
  204.         if err = noErr then begin
  205.             err := err2;
  206.         end; (* if *)
  207.         err2 := AEPutKeyPtr(theEvent, '----', 'TEXT', @key[1], length(key));
  208.         if err = noErr then begin
  209.             err := err2;
  210.         end; (* if *)
  211.         err2 := AEPutKeyPtr(theEvent, 'dest', 'fss ', @prefsfile, sizeof(prefsfile));
  212.         if err = noErr then begin
  213.             err := err2;
  214.         end; (* if *)
  215.         if err <> noErr then begin
  216.             junk := AEDisposeDesc(theEvent);
  217.         end; (* if *)
  218.         junk := AEDisposeDesc(targetAddress);
  219.         CreateEditPrefEvent := err;
  220.     end; (* CreateEditPrefEvent *)
  221.  
  222.     function LaunchFSSpec (var fs: FSSpec; theEvent: AppleEvent): ICError;
  223.         var
  224.             launchThis: LaunchParamBlockRec;
  225.             launchDesc: AEDesc;
  226.             err: ICError;
  227.     begin
  228.         launchThis.launchAppSpec := @fs;
  229.         err := PrepareToLaunch(theEvent, true, launchThis);
  230.         if err = noErr then begin
  231.             err := LaunchApplication(@launchThis);
  232.         end; (* if *)
  233.         if err = memFullErr then begin
  234.             launchThis.launchControlFlags := bor(launchThis.launchControlFlags, launchUseMinimum);
  235.             err := LaunchApplication(@launchThis);
  236.         end; (* if *)
  237.         LaunchFSSpec := err;
  238.     end; (* LaunchFSSpec *)
  239.  
  240.     function SendEvent (theEvent: AppleEvent; creator: OSType): ICError;
  241.         var
  242.             err: ICError;
  243.             psn: ProcessSerialNumber;
  244.             app_fs: FSSpec;
  245.             junk: ICError;
  246.             reply: AppleEvent;
  247.     begin
  248.         if FindProcess(creator, 'APPL', psn, app_fs) then begin
  249.             junk := SetFrontProcess(psn);
  250.             err := AESend(theEvent, reply, kAENoReply, kAEHighPriority, kNoTimeOut, nil, nil);
  251.         end
  252.         else begin
  253.             err := FindApplication(creator, app_fs);
  254.             if err = noErr then begin
  255.                 err := LaunchFSSpec(app_fs, theEvent);
  256.             end; (* if *)
  257.         end; (* if *)
  258.         SendEvent := err;
  259.     end; (* SendEvent *)
  260.  
  261.     function EditPreferences (key: Str255; prefsfile: FSSpec): ICError;
  262.         var
  263.             err: ICError;
  264.             junk: ICError;
  265.             theEvent: AppleEvent;
  266.     begin
  267.         err := HaveProcessManager;
  268.         if err = noErr then begin
  269.             err := CreateEditPrefEvent(ICcreator, key, prefsfile, theEvent);
  270.             if err = noErr then begin
  271.                 err := SendEvent(theEvent, ICcreator);
  272.             end; (* if *)
  273.             junk := AEDisposeDesc(theEvent);
  274.         end; (* if *)
  275.         EditPreferences := err;
  276.     end; (* EditPreferences *)
  277.  
  278.     function FindScheme (urlh: Handle; var scheme: Str255): ICError;
  279.         var
  280.             err: ICError;
  281.             tmp: Str15;
  282.             ndx: longint;
  283.     begin
  284.         err := noErr;
  285.         tmp := ':';
  286.         ndx := Munger(Handle(urlh), 0, @tmp[1], length(tmp), nil, 0);
  287.         if (ndx < 0) or (ndx > 255) then begin
  288.             err := icNoURLErr;
  289.         end; (* if *)
  290.         if err = noErr then begin
  291. {$push}
  292. {$r-}
  293.             scheme[0] := chr(ndx);
  294.             BlockMove(urlh^, @scheme[1], ndx);
  295. {$pop}
  296.         end; (* if *)
  297.         FindScheme := err;
  298.     end; (* FindScheme *)
  299.  
  300.     function LaunchURL (helper: OSType; urlh: Handle): ICError;
  301.         var
  302.             err: ICError;
  303.             junk: ICError;
  304.             theEvent: AppleEvent;
  305.     begin
  306.         err := HaveProcessManager;
  307.         if err = noErr then begin
  308.             err := CreateGURLEvent(helper, urlh, theEvent);
  309.             if err = noErr then begin
  310.                 err := SendEvent(theEvent, helper);
  311.             end; (* if *)
  312.             junk := AEDisposeDesc(theEvent);
  313.         end; (* if *)
  314.         LaunchURL := err;
  315.     end; (* LaunchURL *)
  316.  
  317. end. (* ICRSubs *)